package com.pyg.goods.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.pyg.pojo.TbContentExample;
import com.pyg.pojo.TbContentExample.Criteria;
import com.pyg.mapper.TbContentMapper;
import com.pyg.pojo.TbContent;
import com.pyg.goods.service.TbContentService;

import com.pyg.utils.PygStringUtils;
import entity.PageResult;

/**	*  @Author zhuliangcai
*  @Email zhuliangcai@itcast.cn
*  @Date 2018-04-03 20:07:45
*/
@Service
public class TbContentServiceImpl implements TbContentService {
	@Autowired
	private TbContentMapper tbContentMapper;

	@Override
	public List<TbContent> findAll() {
		return tbContentMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		Page<TbContent> page = (Page<TbContent>) tbContentMapper.selectByExample(null);

		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 品牌条件分页
	 *
	 * @param TbContent
	 *            查询条件
	 * @param pageNum
	 *            当前页面
	 * @param pageSize
	 *            每页记录数
	 * @return
	 */
	@Override
	public PageResult findPage(TbContent tbContent, int pageNum, int pageSize) {
		// 开启分页插件功能
		PageHelper.startPage(pageNum, pageSize);

		TbContentExample example = null;
		if (tbContent != null) {
			example = new TbContentExample();
			Criteria criteria = example.createCriteria();
			if (PygStringUtils.isNotEmpty(tbContent.getCategoryId())) {
				criteria.andCategoryIdEqualTo(tbContent.getCategoryId() );
			}
			if (PygStringUtils.isNotEmpty(tbContent.getTitle())) {
				criteria.andTitleLike("%" + tbContent.getTitle() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbContent.getUrl())) {
				criteria.andUrlLike("%" + tbContent.getUrl() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbContent.getPic())) {
				criteria.andPicLike("%" + tbContent.getPic() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbContent.getStatus())) {
				criteria.andStatusLike("%" + tbContent.getStatus() + "%");
			}
			if (PygStringUtils.isNotEmpty(tbContent.getSortOrder())) {
				criteria.andSortOrderEqualTo(tbContent.getSortOrder() );
			}
		}

		Page<TbContent> page = (Page<TbContent>) tbContentMapper.selectByExample(example);

		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public void add(TbContent tbContent) {
		tbContentMapper.insert(tbContent);
	}

	@Override
	public void del(Long[] ids) {
		for (Long id : ids) {
			tbContentMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public TbContent findOne(Long id) {
		return tbContentMapper.selectByPrimaryKey(id);
	}

	@Override
	public void update(TbContent tbContent) {
		tbContentMapper.updateByPrimaryKey(tbContent);
	}

}
